<template>
	<view class="work-container" style="margin: 0 10px;">
		<!-- 轮播图 -->
		<uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content"
			style="margin: 0 12px;border-radius: 6px;">
			<swiper class="swiper-box" :current="swiperDotIndex" circular="true" @change="changeSwiper" autoplay="true">
				<swiper-item v-for="(item, index) in data" :key="index">
					<view class="swiper-item" @click="clickBannerItem(item)">
						<image :src="item.image" mode="aspectFill" style="height: 150px;width: 100%;"
							:draggable="false" />
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>

		<!-- 宫格组件 -->
		<view class="grid-body">
			<uni-grid :column="2" :showBorder="false">
				<uni-grid-item v-for="(item, index) in menuList" :key="index" style="height: 140px;">
					<view class="grid-item-box" @click="clickGrid(item)">
						<!-- <uni-icons :type="item.icon" size="30"></uni-icons> -->
						<image :src="item.icon" style="width:100%;height: 100%;border-radius: 6px;" />
						<text class="title">{{ item.menuName }}</text>
						<text class="text">{{ item.remark }}</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>

		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	import {
		sansQrCode
	} from '@/api/jz/sign/sign.js';

	export default {
		data() {
			return {
				current: 0,
				swiperDotIndex: 0,
				menuList: [],
				data: [{
						image: '/static/images/banner/cmedu.png'
					}, {
						image: '/static/images/banner/banner01.png'
					},
					{
						image: '/static/images/banner/banner02.png'
					},
					{
						image: '/static/images/banner/banner03.png'
					}
				],
				address: '',
				longitude: "",
				latitude: "",
				whetherPositioningIsEnabled: true
			}
		},
		mounted() {

			this.getLongitudeAndLatitude();

			this.$store.dispatch('GetRouters').then(res => {
				this.menuList = []
				if (res.code == 200) {
					for (var i = 0; i < res.data.length; i++) {
						if (res.data[i].name != "System" && "Monitor" != res.data[i].name && "Tool" != res.data[i]
							.name && "ClassStudent" != res.data[i].name) {
							this.menuList.push(res.data[i])
						}
					}
				}
			})

		},
		methods: {
			clickBannerItem(item) {
				console.info(item)
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			clickGrid(item) {
				if ('/pages/work/sign/scanQrCodes' === item.path) {
					this.sansQrCodes();
					return;
				}
				this.$tab.navigateTo(item.path)
			},
			sansQrCodeCallback(data) {
				sansQrCode(data).then((res) => {
					console.log("res", res)
					if (res.code === 200) {
						this.showSuccess()
					} else {
						this.showError()
					}
				}).catch((err) => {
					console.error(err);
				})
			},
			sansQrCodes() {
				// this.checkOpenGPSService();
				this.getLongitudeAndLatitude();
				let that = this;
				// 只允许通过相机扫码
				uni.scanCode({
					scanType: ['qrCode'],
					onlyFromCamerLa: true,
					success: function(res) {
						var prop = {}
						prop.getData = res.result;
						prop.address = that.address;
						prop.longitude = that.longitude;
						prop.latitude = that.latitude;
						that.sansQrCodeCallback(prop)
					}
				});

			},
			async getLongitudeAndLatitude() {
				let that = this;
				await uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						console.log(res)
						that.longitude = res.longitude;
						that.latitude = res.latitude;
						console.log(that.longitude)
						console.log(that.latitude)
					}
				})
			},
			showSuccess() {
				this.$refs.uToast.show({
					title: '签到成功',
					type: 'success'
				})
			},

			showError() {
				this.$refs.uToast.show({
					title: '签到失败',
					type: 'error'
				})
			},

		}


	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 14px;
		line-height: inherit;
	}

	/* #endif */

	.text {
		text-align: center;
		margin-left: 4px;
		font-size: 12px;
		margin-top: 8rpx;
	}

	.title {
		position: absolute;
		width: 40px;
		height: 40px;
		font-size: 15px;
		font-weight: 500;
		letter-spacing: 0.2em;
		margin: 36px 0 0 8px;
		color: white;
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: flex-start;
		// align-items: center;
		justify-content: center;
		margin: 15px 12px 0 10px;
	}

	.uni-margin-wrap {
		width: 690 rpx;
		width: 100%;
	}

	.swiper {
		height: 300 rpx;
	}

	.swiper-box {
		height: 150px;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300 rpx;
		line-height: 300 rpx;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			// width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}
</style>
